package com.smartmedical.system.patient.controller;

import com.smartmedical.common.core.domain.HealthR;
import com.smartmedical.common.security.utils.SecurityUtils;
import com.smartmedical.system.patient.domain.dto.PatientIserArchivesDto;
import com.smartmedical.system.patient.domain.dto.PatientUserIdCardDto;
import com.smartmedical.system.patient.domain.entity.PatientSystemNoticeEntity;
import com.smartmedical.system.patient.domain.entity.PatientUserConsumptionRecordEntity;
import com.smartmedical.system.patient.domain.entity.PatientUserEntity;
import com.smartmedical.system.patient.domain.entity.PatientUserWalletEntity;
import com.smartmedical.system.patient.domain.vo.*;
import com.smartmedical.system.patient.service.PatientUserService;
import com.smartmedical.system.patient.util.EmailUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @BelongsProject: smart-medical
 * @BelongsPackage: com.smartmedical.controller
 * @ClassName: UserController
 * @Author: zyn
 * @CreateTime: 2024-01-08  16:48
 * @Description: TODO
 */
@RestController
@RequestMapping("/verify")
@Log4j2
public class PatientUserVerifyController {



    @Autowired
    private PatientUserService patientUserService;

    @Autowired
    private EmailUtil emailUtil;


    /**
     * 根据用户ID获取用户信息
     *
     * @param id 用户ID
     * @return 包含用户信息的HealthR对象
     */
    @GetMapping("/v1/getUserInfoById")
    public HealthR<UserInfoByIdVo> UserInfoById(@RequestParam Long id){
        UserInfoByIdVo patientUserEntity = patientUserService.UserInfoById(id);

        return HealthR.ok(patientUserEntity,"查询成功");
    }

    /**
     * 完善用户信息
     *
     * @param age 用户年龄
     * @param height 用户身高
     * @param weight 用户体重
     * @param id 用户ID
     * @return HealthR 健康状态响应
     */
    @PutMapping("/v1/perfectUserInfo")
    public HealthR perfectUserInfo(@RequestParam Integer age ,@RequestParam Integer height,@RequestParam
    Integer weight, @RequestParam Long id ){
        patientUserService.perfectUserInfo(age,height,weight,id);
        return HealthR.ok("", "完善成功");
    }





    /**
     * 添加用户病历档案
     *
     * @param patientIserArchivesDto 患者用户档案DTO
     * @return 返回HealthR对象，表示操作结果
     */
    @PostMapping("/v1/addUserArchives")
    public HealthR addUserArchives(@RequestBody PatientIserArchivesDto patientIserArchivesDto){
        patientUserService.addUserArchives(patientIserArchivesDto);
        return HealthR.ok("","添加成功");
    }

    /**
     * 上传病历图片接口
     *
     * @param picture 图片文件数组
     * @param archivesId 病历ID
     * @return HealthR 健康状态响应
     */
    @PostMapping("/v1/uploadArchivesPicture")
    public HealthR uploadArchivesPicture(@RequestParam  String []  picture ,@RequestParam Long archivesId){
        HealthR healthR = patientUserService.uploadArchivesPicture(picture,archivesId);

        return HealthR.ok("","上传成功");
    }



    /**
     * 查询用户病历存档
     *
     * @return 返回查询结果
     */
    @GetMapping("/v1/findUserArchives")
    public HealthR<PatientIserArchivesVo> findUserArchives(){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;


        HealthR<PatientIserArchivesVo> userArchives = patientUserService.findUserArchives(userId);


        return HealthR.ok(userArchives.getResult(),"查询成功");
    }

    /**
     * 添加签到记录
     *
     * @return 返回一个HealthR对象，表示操作是否成功
     */
    @PostMapping("v1/addSign")
    public HealthR addSign(){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;
        patientUserService.addSign(userId);
        return HealthR.ok("","签到成功");

    }

    /**
     * 判断用户今天是否签到
     *
     * @return 返回查询结果，查询成功返回签到类型，查询失败返回失败信息
     */
    @GetMapping("v1/whetherSignToday")
    public HealthR<Integer> whetherSignToday(){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;
        HealthR<List<PatientUserConsumptionRecordEntity>> healthR = patientUserService.whetherSignToday(userId);
        List<PatientUserConsumptionRecordEntity> result = healthR.getResult();

        for (PatientUserConsumptionRecordEntity patientUserConsumptionRecordEntity : result) {


            Integer type = patientUserConsumptionRecordEntity.getType();

            return HealthR.ok(type,"查询成功");
        }


        return HealthR.fail("查询失败");

    }

    /*
     * 查询用户连续签到天数
     */


    /**
     * 查询用户今日是否签到
     *
     * @return 返回查询结果，若成功则返回签到天数，若失败则返回失败信息
     */
    @GetMapping("v1/findUserSign")
    public HealthR<Integer> findUserSignDays(){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;

        HealthR<List<PatientUserConsumptionRecordEntity>> healthR = patientUserService.whetherSignTodays(userId);
        List<PatientUserConsumptionRecordEntity> result = healthR.getResult();

        if (result != null && !result.isEmpty()) {
            long count = result.stream()
                    .filter(record -> record.getType() == 1)
                    .count();

            return HealthR.ok((int) count, "查询成功");
        } else {
            return HealthR.fail("查询失败");
        }
    }



    /**
     * 更新用户性别信息
     *
     * @param sex 用户性别，取值为0或1
     * @return 返回一个HealthR对象，表示操作是否成功
     */
    @PutMapping("v1/updateUserSex")
    public HealthR updateUserInfo(@RequestParam Integer sex){

        Long userId = SecurityUtils.getUserId();
        userId = 1L;
        patientUserService.updateUserInfo(sex,userId);

        return HealthR.ok("","修改成功");
    }


    /**
     * 修改用户昵称
     *
     * @param nickName 用户新昵称
     * @return 返回修改成功的健康状态
     */
    @PutMapping("v1/modifyNickName")
    public HealthR updateUserNickName(@RequestParam String nickName){

        Long userId = SecurityUtils.getUserId();
        userId = 1L;
        patientUserService.updateUserNickName(nickName,userId);

        return HealthR.ok("","修改成功");
    }



    /**
     * 生成邀请码
     *
     * @return 返回一个 HealthR 对象，表示请求是否成功
     */
    @PostMapping("v1/makeInvitationCode")
    public HealthR makeInvitationCode(){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;

        patientUserService.makeInvitationCode(userId);

        return HealthR.ok("","生成邀请码成功");
    }



    /**
     * 获取用户邀请码
     *
     * @return 返回一个包含邀请码的HealthR对象
     */
    @GetMapping("v1/findUserInvitationCode")
    public HealthR<String> findUserInvitationCode(){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;
        HealthR<List<PatientUserEntity>> healthR = patientUserService.findUserInvitationCode(userId);


        List<PatientUserEntity> result = healthR.getResult();

        for (PatientUserEntity patientUserEntity : result) {


            String invitationCode = patientUserEntity.getInvitationCode();

            return HealthR.ok(invitationCode,"查询成功");
        }

        return HealthR.fail("查询失败");
    }

    /**
     * 更新用户密码
     *
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     * @return 健康状态响应
     */
    @PutMapping("v1/updateUserPwd")
    public HealthR updateUserPwd(@RequestParam String oldPwd, @RequestParam String newPwd){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;
        patientUserService.updateUserPwd(oldPwd,newPwd,userId);
        return HealthR.ok("","修改成功");
    }

    /**
     * 添加用户推送令牌
     *
     * @param token 推送令牌
     * @return 返回健康状态响应
     */
    @PostMapping("v1/addUserPushToken")
    public HealthR sendOutEmailCode(@RequestParam String token){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;


        patientUserService.addPushToken(token,userId);

        return HealthR.ok("","添加成功");
    }




    /**
     * 处理任务请求
     *
     * @param taskId 任务ID
     * @return 返回一个HealthR对象，表示任务处理结果
     */
    @PostMapping("v1/doTask")
    public HealthR doTask(@RequestParam Integer taskId){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;

        patientUserService.doTask(taskId,userId);

        return HealthR.ok("","做任务成功");
    }


    /**
     * 查询用户任务列表
     *
     * @return 返回查询结果，包含用户任务记录
     */
    @GetMapping("v1/findUserTaskList")
    public HealthR<UserTaskRecordVo> findUserTaskList(){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;

        HealthR<UserTaskRecordVo> healthR = patientUserService.findUserTaskList(userId);
        UserTaskRecordVo patientTaskVos = healthR.getResult();

        UserTaskRecordVo userTaskRecordVo = new UserTaskRecordVo();
        userTaskRecordVo.setId(patientTaskVos.getId());
        userTaskRecordVo.setUserId(patientTaskVos.getUserId());
        userTaskRecordVo.setTaskName(patientTaskVos.getTaskName());
        userTaskRecordVo.setTaskType(patientTaskVos.getTaskType());
        userTaskRecordVo.setReward(patientTaskVos.getReward());
        userTaskRecordVo.setCreateTime(patientTaskVos.getCreateTime());
        userTaskRecordVo.setWhetherFinish(2);
        userTaskRecordVo.setWhetherReceive(1);


        return HealthR.ok(userTaskRecordVo,"查询成功");
        }



    /**
     * 领取奖励
     *
     * @param taskId 任务ID
     * @return 返回健康状态响应
     */
    @PostMapping("v1/receiveReward")
    public HealthR receiveReward(@RequestParam Integer taskId){
        Long userId = SecurityUtils.getUserId();
        userId = 1L;

        patientUserService.receiveReward(taskId,userId);

        return HealthR.ok("","已领取，不可重复领取");
    }


    /**
     * 更新用户病历信息
     *
     * @param archives 患者病历信息
     * @return 健康状态响应
     */
    @PutMapping("v1/updateUserArchives")
    public HealthR updateUserArchives(@RequestBody PatientIserArchivesDto archives){

        patientUserService.updateArchives(archives);
        return HealthR.ok(archives,"修改成功");
    }


    /**
     * 删除用户病历
     *
     * @param archivesId 病历ID
     * @return 返回健康状态响应
     */
    @DeleteMapping("v1/deleteUserArchives")
    public HealthR deleteUserArchives(@RequestParam Integer archivesId){

        patientUserService.deleteArchives(archivesId);
        return HealthR.ok("","删除成功");
    }


    /**
     * 添加资讯收藏的API接口
     *
     * @param infoId 资讯ID
     * @return 返回HealthR对象，表示请求结果
     */
    @PostMapping("v1/addInfoCollection")
    public HealthR addInfoCollection(@RequestParam Integer infoId){

        HealthR healthR=    patientUserService.addInfoCollection(infoId);
        return HealthR.ok("","资讯收藏成功");

    }



    /**
     * 查询用户信息集合列表
     *
     * @return 返回查询结果
     */
    @GetMapping("v1/findUserInfoCollectionList")
    public HealthR<List<PatientInfoVo>> findUserInfoCollectionList(){

        HealthR<List<PatientInfoVo>> healthR = patientUserService.findInfoCollections();

        return HealthR.ok(healthR.getResult(),"查询成功");
}


    /**
     * 取消收藏信息
     *
     * @param infoId 信息的ID
     * @return 返回一个HealthR对象，表示请求的结果
     */
     @DeleteMapping("v1/cancelInfoCollection")
    public HealthR cancelInfoCollection(@RequestParam Integer infoId){

        patientUserService.cancelInfoCollection(infoId);
        return HealthR.ok("","取消收藏成功");
     }


    /**
     * 查询视频合集列表
     *
     * @param page 页码，从0开始
     * @param count 每页显示数量
     * @return 查询结果，包括视频合集列表和状态信息
     */
     @GetMapping("v1/findVideoCollectionList")
    public HealthR<List<PatientVideoVo>> findVideoCollectionList(@RequestParam  Integer page, @RequestParam  Integer count){

         HealthR<List<PatientVideoVo>> healthR = patientUserService.findVideoCollectionList(page,count);

         return HealthR.ok(healthR.getResult(),"查询成功");
     }



    /**
     * 取消视频收藏
     *
     * @param videoId 视频ID
     * @return 返回健康状态响应
     */
     @DeleteMapping("v1/cancelVideoCollection")
    public HealthR cancelVideoCollection(@RequestParam Integer videoId){

         patientUserService.cancelVideoCollection(videoId);
         return HealthR.ok("","取消收藏成功");
     }

    /**
     * 查询用户关注的医生列表
     *
     * @param page 页码，从0开始
     * @param count 每页显示数量
     * @return 返回查询结果，包括患者用户医生信息列表和状态信息
     */
     @GetMapping("v1/findUserDoctorFollowList")
    public HealthR<List<PatientUserDoctorVo>> findUserDoctorFollowList(@RequestParam  Integer page, @RequestParam  Integer count){
         HealthR<List<PatientUserDoctorVo>> healthR = patientUserService.findUserDoctorFollowList(page,count);
         return HealthR.ok(healthR.getResult(),"查询成功");
     }


    /**
     * 查询用户视频购买列表
     *
     * @param page 页码，从0开始
     * @param count 每页显示数量
     * @return 返回查询结果，包括患者用户视频列表和状态信息
     */
     @GetMapping("v1/findUserVideoBuyList")
    public HealthR<List<PatientUserVideoVo>> findUserVideoBuyList(@RequestParam  Integer page, @RequestParam  Integer count){
         HealthR<List<PatientUserVideoVo>> healthR = patientUserService.findUserVideoBuyList(page,count);
         return HealthR.ok(healthR.getResult(),"查询成功");
     }


    /**
     * 删除购买视频接口
     *
     * @param videoId 视频ID
     * @return 健康响应
     */
     @DeleteMapping("v1/deleteVideoBuy")
     public HealthR deleteVideoBuy(@RequestParam Integer videoId){

         patientUserService.deleteVideoBuy(videoId);
         return HealthR.ok("","删除成功");
     }

    /**
     * 查询用户消费记录列表
     *
     * @param page 页码，从0开始
     * @param count 每页显示数量
     * @return 返回查询结果，包括健康状态和消费记录列表
     */
     @GetMapping("v1/findUserConsumptionRecordList")
     public HealthR<List<ConsumptionRecordVo>> findUserConsumptionRecordList(@RequestParam Integer page, @RequestParam Integer count){
         HealthR<List<ConsumptionRecordVo>> healthR = patientUserService.findUserConsumptionRecordList(page,count);
         return HealthR.ok(healthR.getResult(),"查询成功");
     }


    /**
     * 查询用户钱包
     *
     * @return 返回查询结果
     */
     @GetMapping("v1/findUserWallet")
     public HealthR<PatientUserWalletEntity> findUserWallet(){
         Long userId = SecurityUtils.getUserId();
         userId = 1L;
         HealthR<PatientUserWalletEntity> healthR = patientUserService.findUserWallet(userId);

         return HealthR.ok(healthR.getResult(),"查询成功");
     }

    /**
     * 验证验证码的接口
     *
     * @param email 邮箱地址
     * @param code 验证码
     * @return HealthR对象，包含请求的结果
     */
     @PostMapping("v1/checkCode")
     public HealthR checkCode(@RequestParam String email, @RequestParam String code){


         HealthR healthR = patientUserService.checkCode(email,code);

         return HealthR.ok("","验证码正确");

     }



    /**
     * 重置用户密码
     *
     * @param email 邮箱地址
     * @param pwd1 密码1
     * @param pwd2 密码2
     * @return HealthR 健康状态响应
     */
     @PutMapping("v1/resetUserPwd")
    public HealthR resetUserPwd(@RequestParam String email, @RequestParam String pwd1, @RequestParam String pwd2){

         patientUserService.resetUserPwd(email,pwd1,pwd2);
         return HealthR.ok("","修改密码成功");
}




    /**
     * 查询系统公告列表
     *
     * @param page 页码，默认为第一页
     * @param count 每页数量，默认为10条
     * @return 查询结果
     */
    /**
     * 查询系统公告列表
     *
     * @param page 页码，默认为第一页
     * @param count 每页数量，默认为10条
     * @return 查询结果
     */
    @GetMapping("v1/findSystemNoticeList")
    public HealthR<List<PatientNoticeVo>> findSystemNoticeList(@RequestParam Integer page, @RequestParam Integer count){

         HealthR<List<PatientNoticeVo>> healthR = patientUserService.findSystemNoticeList(page,count);
         return HealthR.ok(healthR.getResult(),"查询成功");

     }


    /**
     * 查询患者咨询通知列表
     *
     * @param page 页码，从0开始
     * @param count 每页显示数量
     * @return 返回查询结果和提示信息
     */
     @GetMapping("v1/findInquiryNoticeList")
     public HealthR<List<PatientInquiryNoticeVo>> findInquiryNoticeList(@RequestParam Integer page, @RequestParam Integer count){
         HealthR<List<PatientInquiryNoticeVo>> healthR = patientUserService.findInquiryNoticeList(page,count);
         return HealthR.ok(healthR.getResult(),"查询成功");
     }


    /**
     * 查询健康币通知列表
     *
     * @param page 页码，从0开始
     * @param count 每页显示数量
     * @return 返回查询结果，包括健康币通知列表和状态信息
     */
     @GetMapping("v1/findHealthyCurrencyNoticeList")
     public HealthR<List<CurrencyNoticeVo>> findHealthyCurrencyNoticeList(@RequestParam Integer page, @RequestParam Integer count){
         HealthR<List<CurrencyNoticeVo>> healthR = patientUserService.findHealthyCurrencyNoticeList(page,count);
         return HealthR.ok(healthR.getResult(),"查询成功");
     }


    /**
     * 绑定用户银行卡
     *
     * @param bankCardNumber 银行卡号
     * @param bankName 银行名称
     * @param bankCardType 银行卡类型
     * @return HealthR 健康状态响应
     */
     @PostMapping("v1/bindUserBankCard")
     public HealthR bindUserBankCard(@RequestParam String bankCardNumber,@RequestParam String bankName,@RequestParam String bankCardType){
         patientUserService.bindUserBankCard(bankCardNumber,bankName,bankCardType);
         return HealthR.ok("","绑定成功");
     }



    /**
     * 根据用户ID查询用户银行卡信息
     *
     * @return 返回查询结果和状态信息
     */
     @GetMapping("v1/findUserBankCardByUserId")
     public HealthR<PatientBankCardInfoVo> findUserBankCardByUserId(){
         	Long userId = SecurityUtils.getUserId();
             	userId = 1L;
                 	HealthR<PatientBankCardInfoVo> healthR = patientUserService.findUserBankCardByUserId(userId);
                     	return HealthR.ok(healthR.getResult(),"查询成功");
     }


    /**
     * 绑定用户身份证号
     *
     * @param patientUserIdCardDto 患者用户身份证号DTO
     * @return 返回健康状态响应对象
     */
     @PostMapping("v1/bindUserIdCard")
     public HealthR bindUserIdCard(@RequestBody PatientUserIdCardDto patientUserIdCardDto){

         	patientUserService.bindUserIdCard(patientUserIdCardDto);
             	return HealthR.ok(patientUserIdCardDto,"绑定成功");
     }


    /**
     * 查询用户身份证信息
     *
     * @return 返回查询结果
     */
     @GetMapping("v1/findUserIdCard")
     public HealthR<PatientUserIdCardVo> findUserIdCard(){
         	Long userId = SecurityUtils.getUserId();
             userId = 1L;
             	HealthR<PatientUserIdCardVo> healthR = patientUserService.findUserIdCard(userId);
                 	return HealthR.ok(healthR.getResult(),"查询成功");
     }



    /**
     * 获取用户提现记录列表
     *
     * @param page 页码，从0开始
     * @param count 每页显示数量
     * @return 返回用户提现记录列表
     */
     @GetMapping("v1/findUserDrawRecordList")
    public HealthR<List<PatientUserDrawRecordVo>> findUserDrawRecordList(@RequestParam Integer page, @RequestParam Integer count){
         HealthR<List<PatientUserDrawRecordVo>> healthR = patientUserService.findUserDrawRecordList(page,count);
         return HealthR.ok(healthR.getResult(),"查询成功");
     }


    /**
     * 提现接口
     *
     * @param money 提现金额
     * @return 健康响应体
     */
     @PostMapping("verify/v1/drawCash")
     public HealthR drawCash(@RequestParam Integer money){
         	Long userId = SecurityUtils.getUserId();
             	userId = 1L;
         	patientUserService.drawCash(money,userId);
             	return HealthR.ok("","提现成功");
     }

    /**
     * 获取奖励信息
     *
     * @param infoId 奖励信息ID
     * @return 返回一个HealthR对象，表示请求是否成功
     */
     @PostMapping("v1/watchInfoRewards")
     public HealthR watchInfoRewards(@RequestParam Integer infoId){
         	Long userId = SecurityUtils.getUserId();
	        userId = 1L;
	patientUserService.watchInfoRewards(infoId,userId);
    	return HealthR.ok("","获得奖励成功");

     }

    /**
     * 查询用户未读通知数量
     *
     * @return 返回包含未读通知数量的List的HealthR对象
     */
    @GetMapping("v1/findUserNoticeReadNum")
    public HealthR<List<PatientUserNoticeReadNum>> findUserNoticeReadNum() {
        try {
            List<PatientSystemNoticeEntity> patientSystemNoticeEntityList = patientUserService.findUserNoticeReadNum();

            int count = 0;
            for (PatientSystemNoticeEntity patientSystemNoticeEntity : patientSystemNoticeEntityList) {
                if (patientSystemNoticeEntity.getStatus() == 1) {
                    log.info("消息已读");
                }
                count++;
            }

            PatientUserNoticeReadNum patientUserNoticeReadNum = new PatientUserNoticeReadNum();
            // 假设patientSystemNoticeEntityList不为空，这里取第一个元素的noticeType作为示例
            patientUserNoticeReadNum.setNoticeType(patientSystemNoticeEntityList.get(0).getNoticeType());
            patientUserNoticeReadNum.setNotReadNum(count);

            return HealthR.ok(Collections.singletonList(patientUserNoticeReadNum), "查询成功");
        } catch (Exception e) {
            log.error("查询用户通知未读数失败", e);
            return HealthR.fail("查询失败");
        }
    }

    @PutMapping("v1/modifyAllStatus")
    public HealthR modifyAllStatus(){
        // 修改所有用户状态为已审核
        Long userId = SecurityUtils.getUserId();
        userId = 1L;
        patientUserService.modifyAllStatus(userId);
        return HealthR.ok("","修改成功");
    }

   @GetMapping("v1/userHealthTest")
    public HealthR<String> userHealthTest(){
        // 修改所有用户状态为已审核

        return patientUserService.userHealthTest();
    }


















}





















